package com.ubo.transform;

import org.jdom2.Element;

import com.ubo.java.associations.Card;
import com.ubo.java.associations.EAssociationCard;
import com.ubo.java.associations.EAssociationType;
import com.ubo.java.model.EModifier;
import com.ubo.java.model.EVisibility;
import com.ubo.java.model.classifier.EPrimitiveTypes;
import com.ubo.uspeed.ETypeClassifier;

public abstract class AAnalyser {
	/**
	 * Renvoie la visibilité de l'element
	 * 
	 * @param e
	 * @return
	 */
	protected EVisibility elementVisibility(Element e) {
		if (e.getAttributeValue("visibility") != null) {
			if (e.getAttributeValue("visibility").toString()
					.compareTo("public") == 0)
				return EVisibility.PUBLIC;
			else if (e.getAttributeValue("visibility").toString()
					.compareTo("public") == 0)
				return EVisibility.PRIVATE;
			else
				return EVisibility.PROTECTED;
		} else {
			return EVisibility.PUBLIC;
		}
	}

	/**
	 * Renvoie la visibilité correspondant au string
	 * 
	 * @param e
	 * @return
	 */
	protected EVisibility stringVisibility(String v) {
		if (v.startsWith("public"))
			return EVisibility.PUBLIC;
		else if (v.startsWith("private"))
			return EVisibility.PRIVATE;
		else if (v.startsWith("protected"))
			return EVisibility.PROTECTED;
		else
			return null;
	}

	/**
	 * Renvoie le modifier de l'element
	 * 
	 * @param e
	 * @return
	 */
	protected EModifier elementModifier(Element e) {
		if (e.getAttributeValue("modifier") != null) {
			if (e.getAttributeValue("modifier").toString()
					.compareTo("abstract") == 0)
				return EModifier.ABSTRACT;
			else if (e.getAttributeValue("modifier").toString()
					.compareTo("final") == 0)
				return EModifier.FINAL;
			else
				return EModifier.STATIC;
		} else {
			return null;
		}
	}

	/**
	 * Renvoie le modifier de l'element
	 * 
	 * @param e
	 * @return
	 */
	protected EModifier stringModifier(String v) {
		if (v.startsWith("abstract"))
			return EModifier.ABSTRACT;
		else if (v.startsWith("final"))
			return EModifier.FINAL;
		else if (v.startsWith("static"))
			return EModifier.STATIC;
		else
			return null;
	}

	/**
	 * Renvoie le type de l'element
	 * 
	 * @param e
	 * @return
	 */
	protected EPrimitiveTypes elementPrimitiveType(Element e) {
		if (e.getAttributeValue("type") != null) {
			if (e.getAttributeValue("type").toString().compareTo("boolean") == 0)
				return EPrimitiveTypes.BOOLEAN;
			else if (e.getAttributeValue("type").toString().compareTo("byte") == 0)
				return EPrimitiveTypes.BYTE;
			else if (e.getAttributeValue("type").toString().compareTo("char") == 0)
				return EPrimitiveTypes.CHAR;
			else if (e.getAttributeValue("type").toString().compareTo("double") == 0)
				return EPrimitiveTypes.DOUBLE;
			else if (e.getAttributeValue("type").toString().compareTo("float") == 0)
				return EPrimitiveTypes.FLOAT;
			else if (e.getAttributeValue("type").toString().compareTo("int") == 0)
				return EPrimitiveTypes.INT;
			else
				return EPrimitiveTypes.VOID;
		} else {
			return null;
		}
	}

	/**
	 * Renvoie le type de l'element
	 * 
	 * @param e
	 * @return
	 */
	protected EPrimitiveTypes stringPrimitiveType(String v) {
		if (v.startsWith("boolean"))
			return EPrimitiveTypes.BOOLEAN;
		else if (v.startsWith("byte"))
			return EPrimitiveTypes.BYTE;
		else if (v.startsWith("char"))
			return EPrimitiveTypes.CHAR;
		else if (v.startsWith("double"))
			return EPrimitiveTypes.DOUBLE;
		else if (v.startsWith("float"))
			return EPrimitiveTypes.FLOAT;
		else if (v.startsWith("int"))
			return EPrimitiveTypes.INT;
		else if (v.startsWith("void"))
			return EPrimitiveTypes.VOID;
		else
			return null;
	}

	/**
	 * Renvoie le type d'association
	 * 
	 * @param e
	 * @return
	 */
	protected EAssociationType elementAssociationType(Element e) {
		if (e.getAttributeValue("type") != null) {
			if (e.getAttributeValue("type").toString().compareTo("agregation") == 0)
				return EAssociationType.AGREGATION;
			else if (e.getAttributeValue("type").toString()
					.compareTo("composition") == 0)
				return EAssociationType.COMPOSITION;
			else if (e.getAttributeValue("type").toString()
					.compareTo("extends") == 0)
				return EAssociationType.EXTENDS;
			else if (e.getAttributeValue("type").toString()
					.compareTo("implements") == 0)
				return EAssociationType.IMPLEMENTS;
			else
				return EAssociationType.SIMPLE;

		} else {
			return null;
		}
	}

	/**
	 * Renvoie le type d'association
	 * 
	 * @param e
	 * @return
	 */
	protected EAssociationType stringAssociationType(String v) {
		if (v.startsWith("agregation"))
			return EAssociationType.AGREGATION;
		else if (v.startsWith("composition"))
			return EAssociationType.COMPOSITION;
		else if (v.startsWith("extends"))
			return EAssociationType.EXTENDS;
		else if (v.startsWith("implements"))
			return EAssociationType.IMPLEMENTS;
		else if (v.startsWith("simple"))
			return EAssociationType.SIMPLE;
		else
			return null;
	}

	/**
	 * Renvoie la cardinalite de l'element
	 * 
	 * @param e
	 * @return
	 */
	protected Card elementCard(Element e, String focus) {
		Card res = null;
		if (e.getAttributeValue(focus) != null) {
			String cardString = e.getAttributeValue(focus);
			if(cardString.contains("..")) {
				res = new Card(EAssociationCard.wrapp(""+cardString.charAt(0)),
						EAssociationCard.wrapp(""+cardString.charAt(3)));
			} else {
				res = new Card(EAssociationCard.ZERO,
						EAssociationCard.wrapp(e.getAttributeValue(focus)));
			}
			
			/*
			 * if (e.getAttributeValue(focus).toString().length() > 1) { if
			 * (e.getAttributeValue(focus).equals("N")) res= new
			 * Card(EAssociationCard.N, EAssociationCard.N); else if
			 * (e.getAttributeValue(focus).equals("UN")) res= new
			 * Card(EAssociationCard.UN, EAssociationCard.UN); else res= new
			 * Card(EAssociationCard.ZERO, EAssociationCard.ZERO); } else {
			 * String card = e.getAttributeValue(focus).toString();
			 * EAssociationCard e1; EAssociationCard e2; // Récupere la card
			 * min if (card.startsWith("N")) e1 = EAssociationCard.N; else if
			 * (card.startsWith("UN")) e1 = EAssociationCard.UN; else e1 =
			 * EAssociationCard.ZERO; // Récupere la card max if
			 * (card.endsWith("UN")) e2 = EAssociationCard.UN; else if
			 * (card.endsWith("N")) e2 = EAssociationCard.N; else e2 =
			 * EAssociationCard.ZERO; res= new Card(e1, e2); }
			 */

		}
		return res;
	}

	/**
	 * Renvoie le type du classifier
	 * 
	 * @param e
	 * @return
	 */
	protected ETypeClassifier stringETypeClassifier(String v) {
		if (v.startsWith("class"))
			return ETypeClassifier.CLASS;
		else if (v.startsWith("interface"))
			return ETypeClassifier.INTERFACE;
		else
			return null;
	}
}
